#include <thread>
#include "Util.hpp"
#include "Config.hpp"
#include "Manager.hpp"
#include "Hot.hpp"
#include "Service.hpp"

using std::string;
using std::cout;
using std::endl;
using std::vector;

Cloude::DataManager* _dataM = new Cloude:: DataManager;




void test_Util(const string& filename)
{
	// a.txt
	Cloude::FileUtil fu(filename);
	// test 1
	//cout << fu.GetATime()<< endl;
	//cout << fu.GetMTime()<< endl;
	//cout << fu.GetFileSize()<< endl;

	//// a.txt -->  a.txt.lz
	//string pack_name = filename + ".lz";
	//fu.Compress(pack_name);
	//
	//Cloude::FileUtil fu2(pack_name);
	////a.txt.lz --> unpack_a.txt
	//string backup_name = "unpack_" + filename;
	//fu2.UnCompress(backup_name);

	// test 2
	cout << fu.IsExist() << endl;
	fu.CreateDirectory();
			
	vector<string> arry;
	fu.ScanDirectory(&arry);
	for(auto& file : arry)
	{
		cout << file << endl;
		Cloude::FileUtil f(file);
		cout <<	f.FileName() << endl;
		f.RemoveFile();
	}
	
}



void test_JsonUtil(const string& filename)
{
	Cloude::FileUtil fu(filename);
	
	const char *name = "小明";
    int age = 18;
    float score[] = {88.5, 98, 58};
    Json::Value val;
    val["姓名"] = name;
    val["年龄"] = age;
    val["成绩"].append(score[0]);
    val["成绩"].append(score[1]);
    val["成绩"].append(score[2]);
	
	string str;
	Cloude::JsonUtil::Serialize(&str,val);
	fu.SetContent(str);


	Json::Value root;
	Cloude::JsonUtil::DisSerialize(str,&root);
	cout << root["姓名"].asString() << endl;
	cout << root["年龄"].asString() << endl;
	int sz = root["成绩"].size();
	for(int i = 0; i < sz; i++)
	{
		cout << root["成绩"][i].asFloat() << endl;
	}

}


void test_config()
{
	Cloude::Config* config = Cloude::Config::GetInstance(); 
	cout << config->GetHTime() << endl;
	cout << config->GetBackDir() << endl;
	cout << config->GetPackDir() << endl;
	cout << config->GetPackSuff() << endl;
	cout << config->GetUrlPre() << endl;
	cout << config->GetMFile() << endl;
	cout << config->GetServerIp() << endl;
	cout << config->GetServerPort() << endl;

}


void test_Manager()
{
	cout << "Data_Manager is Running,,," << endl;
	sleep(1);

	// 模拟一次上传请求  -- 最后可以复用
	std::string url = "/download/bundle.h";
	std::string url2 = "/download/httplib.h";

	Cloude::FileUtil fu(url);
	std::string filename = fu.FileName();
	
	Cloude::FileUtil fu2(url2);
	std::string filename2 = fu2.FileName();

	// 创建备份信息结构体
	Cloude::BackupInfo info;
	Cloude::BackupInfo info2;

	Cloude::Config* config = Cloude::Config::GetInstance();
	// a.txt --> /back_dir/a.txt
	std::string real_path = config->GetBackDir() + filename;
	std::string real_path2 = config->GetBackDir() + filename2;
	

	// 创建备份文件，并把对应数据写入
	Cloude::FileUtil f(real_path);
	Cloude::FileUtil f2(real_path2);

	//std::string body = "hello a.txt";
	//std::string body2 = "hello b.txt";

	//f.SetContent(body);
	//f2.SetContent(body);

	// 填充info字段
	info.NewBackupInfo(real_path);
	info2.NewBackupInfo(real_path2);

	_dataM->Insert(info);
	_dataM->Insert(info2);

	//cout << "-------------GetOneByUrl------------------" << endl;
	Cloude::BackupInfo in;
	_dataM->GetOneByUrl(url,&in);
	//in.Print();

	//cout << "-------------GetOneByRealPath--------------" << endl;
	Cloude::BackupInfo in2;
	_dataM->GetOneByRealPath(real_path2,&in2);
	//in2.Print();

	//cout << "--------------GetAll---------------------------" << endl;
	std::vector<Cloude::BackupInfo> arry;
	_dataM->GetAll(&arry);
	//for(int i = 0; i < arry.size(); i++)
	//{
	//	arry[i].Print();
	//	cout << endl;
	//}

}

void test_HotManager()
{
	cout << "HotManager is Running..." << endl;
	Cloude::HotManager hotM;
	hotM.RunMoudle();
}


void test_Server()
{
	Cloude::Service().RunMoudle();
}


void test()
{
	//if(argc < 2)
	//	std::cout << "./cloude filename" << std::endl;
	//test_Util(argv[1]);
	//test_JsonUtil(argv[1]);
	
	//test_config();

	//test_Manager();

	//std::thread pHot(test_HotManager);
	//std::thread pData(test_Manager);
	//pHot.join();
	//pData.join();

	//test_Server();
}

void HotFileManager()
{
	Cloude::HotManager().RunMoudle();
}

void ServiceManager()
{
	Cloude::Service().RunMoudle();
}

		
int main(int argc, char *argv[])
{
	std::thread tHotM(HotFileManager);
	std::thread tSerM(ServiceManager);
	tHotM.join();
	tSerM.join();
	return 0;
}


